<?php
//Helper na vypisovanie hlavneho menu
class MenuHelper extends AppHelper
{
	public $helpers = array('Html', 'Session');
	
	//polozky menu pre prihlaseneho usera
	protected $adminItems = array(
		LABEL_PROJECTS => array(
			'link' => array('admin' => true, 'controller' => 'projects', 'action' => 'list'),
			'submenu' => array(
				LABEL_LIST => array('admin' => true, 'controller' => 'projects', 'action' => 'list'),
				LABEL_ADD_NEW => array('admin' => true, 'controller' => 'projects', 'action' => 'add')
			)
		),
		LABEL_CALLS => array(
			'link' => array('admin' => true, 'controller' => 'calls', 'action' => 'list'),
			'submenu' => array(
				LABEL_LIST => array('admin' => true, 'controller' => 'calls', 'action' => 'list'),
				LABEL_ADD_NEW => array('admin' => true, 'controller' => 'calls', 'action' => 'add')
			)
		),
		LABEL_DEPARTMENTS => array(
			'link' => array('admin' => true, 'controller' => 'departments', 'action' => 'list'),
			'submenu' => array(
				LABEL_LIST => array('admin' => true, 'controller' => 'departments', 'action' => 'list'),
				LABEL_ADD_NEW => array('admin' => true, 'controller' => 'departments', 'action' => 'add')
			)
		),
		LABEL_PEOPLE => array(
			'link' => array('admin' => true, 'controller' => 'people', 'action' => 'list'),
			'submenu' => array(
				LABEL_LIST => array('admin' => true, 'controller' => 'people', 'action' => 'list'),
				LABEL_ADD_NEW => array('admin' => true, 'controller' => 'people', 'action' => 'add')
			)
		),
		LABEL_AGENCIES => array(
			'link' => array('admin' => true, 'controller' => 'agencies', 'action' => 'list'),
			'submenu' => array(
				LABEL_LIST => array('admin' => true, 'controller' => 'agencies', 'action' => 'list'),
				LABEL_ADD_NEW => array('admin' => true, 'controller' => 'agencies', 'action' => 'add')
			)
		),
		LABEL_USERS => array(
			'link' => array('admin' => true, 'controller' => 'users', 'action' => 'list'),
			'submenu' => array(
				LABEL_LIST => array('admin' => true, 'controller' => 'users', 'action' => 'list'),
				LABEL_ADD_NEW => array('admin' => true, 'controller' => 'users', 'action' => 'add')
			)
		),
	);
	
	//polozky menu pre neprihlaseneho usera
	protected $menuItems = array(
		LABEL_PROJECTS => array('controller' => 'projects', 'action' => 'index'),
		LABEL_CALLS => array('controller' => 'calls', 'action' => 'index')
	);
	
	//renderovanie menu
	public function render() {
		
		if ($this->Session->check('Auth.User'))
			return $this->_adminMenu();
		return $this->_menu();
		
	}
	
	//funkcia na generovanie menu pre neprihlaseneho usera
	private function _menu()
	{
		$links 		= array();
		
		foreach ($this->menuItems as $label => $path)
		{			
			
			$selected = (($this->request->params['controller'] == $path['controller']) ? true : false);
			
			$links[] = '<li class="'.($selected ? 'active' : '').'">'.$this->Html->link($label, $path, array('escape' => false)). '</li>';
		}

		return '<div class="menu"><ul>'.implode("\n", $links).'</ul></div>';
	}
	
	//funkcia na generovanie admin menu
	private function _adminMenu()
	{
		$links 		= array();
		
		foreach ($this->adminItems as $label => $item)
		{			
			//ak nie je prihlaseny superadmin, tak sa polozka Users nepridava
			if ($this->Session->read('Auth.User.userlevel') == USER_LEVEL_ADMIN && $label == LABEL_USERS)
				break;
			
			$selected = (($this->request->params['controller'] == $item['link']['controller']) ? true : false);
			
			$submenuLinks = array();
			
			foreach ($item['submenu'] as $submenuLabel => $path)
				$submenuLinks[] = '<li class="">'.$this->Html->link($submenuLabel, $path, array('escape' => false)).'</li>';
			
			$submenu = '<div class="submenu"><ul>'.implode("\n", $submenuLinks).'</ul></div>';
			
			$links[] = '<li class="'.($selected ? 'active' : '').'">'.$this->Html->link($label, $item['link'], array('escape' => false)). $submenu . '</li>';
		}

		return '<div class="menu"><ul>'.implode("\n", $links).'</ul></div>';
	}
}

?>